//
//  LRXMyHelpItemController.m
//  路人侠
//
//  Created by zhangxulong on 4/1/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXMyHelpItemController.h"
#import "LRXMyHelpItemCell.h"
#import "LRXHelpMoreInfoView.h"
#import "MJRefresh.h"
#import "LRXJudgeMentController.h"
#import "LRXOrderMoreView.h"
#import "LRXAlerView.h"
#import "LRXPayViewController.h"
#import "LRXMyHelpOrderModel.h"
#import "LRXRequestHistoryView.h"

@interface LRXMyHelpItemController ()<UIScrollViewDelegate,RequestHistoryViewDelegate>
@property (nonatomic,weak) UIScrollView * contentScrollView;

@property (nonatomic,weak) UIView * spot;
@property (nonatomic,weak) UIButton * AccpBtn;
@property (nonatomic,weak) UIButton * unAccpBtn;
@property (nonatomic,weak) UIButton * sendingBtn;
@property (nonatomic,strong) UIDynamicAnimator * animator;
@property (nonatomic,weak) LRXRequestHistoryView * accpetView;
@end

@implementation LRXMyHelpItemController
- (UIDynamicAnimator *)animator
{
    if (_animator == nil) {
        _animator = [[UIDynamicAnimator alloc]initWithReferenceView:self.view];
    }
    return _animator;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = mainColor;
    self.navigationItem.title = @"我的求带";
    //设置头部
    [self layoutHeadView];
    //设置内容视图
    [self layoutContentView];


}
//头部的设置
- (void)layoutHeadView
{
    CGFloat interW = 200 * wScale;
    UIButton * sendingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sendingBtn setTitle:@"配送中" forState:UIControlStateNormal];
    sendingBtn.titleLabel.font = [UIFont systemFontOfSize:28 * hScale];
    [sendingBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [sendingBtn setTitleColor:[UIColor colorWithRed:181 green:208 blue:252] forState:UIControlStateNormal];
    sendingBtn.frame = CGRectMake(155 * wScale, 54 * hScale, 100 * wScale, 27 * hScale);
    sendingBtn.center = CGPointMake(MainScreenW * 0.5 - interW, 67 * hScale);
    sendingBtn.tag = 1;
    self.sendingBtn = sendingBtn;
    [sendingBtn addTarget:self action:@selector(changItemState:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton * unAccBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [unAccBtn setTitle:@"未完成" forState:UIControlStateNormal];
    unAccBtn.titleLabel.font = [UIFont systemFontOfSize:28 * hScale];
    [unAccBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [unAccBtn setTitleColor:[UIColor colorWithRed:181 green:208 blue:252] forState:UIControlStateNormal];
    unAccBtn.frame = CGRectMake(MainScreenW * 0.5 - 45 * wScale, 54 * hScale, 100 * wScale, 27 * hScale);
    unAccBtn.center = CGPointMake(MainScreenW * 0.5, 67 * hScale);
    unAccBtn.tag = 2;
    self.unAccpBtn = unAccBtn;
    [unAccBtn addTarget:self action:@selector(changItemState:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * AccBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [AccBtn setTitle:@"已完成" forState:UIControlStateNormal];
    AccBtn.titleLabel.font = [UIFont systemFontOfSize:28 * hScale];
    [AccBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [AccBtn setTitleColor:[UIColor colorWithRed:181 green:208 blue:252] forState:UIControlStateNormal];
    AccBtn.frame = CGRectMake(406 * wScale, 54 * hScale, 100 * wScale, 27 * hScale);
    AccBtn.center = CGPointMake(MainScreenW * 0.5 + interW, 67 * hScale);
    AccBtn.tag = 3;
    self.AccpBtn = AccBtn;
    [AccBtn addTarget:self action:@selector(changItemState:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    UIView * spot = [[UIView alloc]init];
    spot.backgroundColor = [UIColor colorWithRed:239 green:211 blue:24];
    CGFloat spotX = sendingBtn.center.x - 5 * hScale;
    CGFloat spotY = CGRectGetMaxY(unAccBtn.frame) + 12 * hScale;
    spot.frame = CGRectMake(spotX, spotY, 10 * hScale, 10 * hScale);
    spot.layer.cornerRadius = 5 * hScale;
    self.spot = spot;
    
    [self.view addSubview:spot];
    [self.view addSubview:unAccBtn];
    [self.view addSubview:AccBtn];
    [self.view addSubview:sendingBtn];
    sendingBtn.selected = YES;
}

- (void)changItemState:(UIButton *)btn
{
    [UIView animateWithDuration:0.5 animations:^{
        self.contentScrollView.contentOffset = CGPointMake((btn.tag - 1) * self.contentScrollView.frame.size.width, 0);
    }];
}

- (void)layoutContentView
{
    UIScrollView * contentScrollView = [[UIScrollView alloc]init];
    contentScrollView.frame = CGRectMake(0, 111 * hScale, MainScreenW, MainScreenH - statuesH - NavbarH - 111 * hScale);
    contentScrollView.backgroundColor = [UIColor whiteColor];
    contentScrollView.contentSize = CGSizeMake(MainScreenW * 3, 0);
    contentScrollView.pagingEnabled = YES;
    contentScrollView.delegate = self;
    contentScrollView.tag = 0;
    contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView = contentScrollView;
    [self.view addSubview:contentScrollView];
    
    [self layoutContentSubView];
    
    
}

//contentView 的子View
- (void)layoutContentSubView
{
    
    LRXRequestHistoryView * sendingTableView = [[LRXRequestHistoryView alloc]initWithFrame:CGRectMake(0, 0, MainScreenW, MainScreenH - statuesH - NavbarH - 111 * hScale)];
    [sendingTableView layoutRequestHistoryView:@"0"];
    sendingTableView.requestHistoryDelegate = self;
    
    LRXRequestHistoryView * unAccTableView = [[LRXRequestHistoryView alloc]initWithFrame:CGRectMake(MainScreenW, 0, MainScreenW, MainScreenH - statuesH - NavbarH - 111 * hScale)];
    [unAccTableView layoutRequestHistoryView:@"1"];
    unAccTableView.requestHistoryDelegate = self;
    
    LRXRequestHistoryView * AccTableView = [[LRXRequestHistoryView alloc]initWithFrame:CGRectMake(MainScreenW * 2, 0, MainScreenW, MainScreenH - statuesH - NavbarH - 111 * hScale)];
    [AccTableView layoutRequestHistoryView:@"2"];
    
    self.accpetView = AccTableView;
    
    [self.contentScrollView addSubview:sendingTableView];
    [self.contentScrollView addSubview:unAccTableView];
    [self.contentScrollView addSubview:AccTableView];
    
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.tag == 0) {
        CGPoint durPoint = self.spot.center;
        NSInteger page = scrollView.contentOffset.x / self.view.frame.size.width + 0.5;
        if (page == 0) {
            self.sendingBtn.selected = YES;
            self.unAccpBtn.selected = NO;
            self.AccpBtn.selected = NO;
            durPoint = CGPointMake(self.sendingBtn.center.x, CGRectGetMaxY(self.sendingBtn.frame) + 12 * hScale);
        }
        else if (page == 1) {
            self.sendingBtn.selected = NO;
            self.unAccpBtn.selected = YES;
            self.AccpBtn.selected = NO;
            durPoint = CGPointMake(self.unAccpBtn.center.x, CGRectGetMaxY(self.unAccpBtn.frame) + 12 * hScale);
        }
        else if(page == 2)
        {
            self.sendingBtn.selected = NO;
            self.unAccpBtn.selected = NO;
            self.AccpBtn.selected = YES;
            durPoint= CGPointMake(self.AccpBtn.center.x, CGRectGetMaxY(self.AccpBtn.frame) + 12 * hScale);
        }
        
        

        //捕捉行为
        [self.animator removeAllBehaviors];
        //取出用户点击的点
        CGPoint point = durPoint;
        //创建一个捕捉行为
        UISnapBehavior * snap = [[UISnapBehavior alloc]initWithItem:self.spot snapToPoint:point];
        //设置捕捉行为的阻力
        snap.damping = 1;
        //在仿真器中 只能存在一个捕捉仿真行为
        [self.animator addBehavior:snap];
        
    }
}

- (void)popRootController
{
    [self.navigationController popViewControllerAnimated:YES];
}

//---------------------------------------------------------------------------------LRXRequestHistoryView 的代理方法
- (void)loadOtherHistroyData
{
    NSLog(@"%s",__func__);
    [self.accpetView loadHistroyData];
}
//-------------------------------------------------------------------------------------------------------------


@end
